package com.lzh.shijicloudstack.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzh.shijicloudstack.mapper.MenuMapper;
import com.lzh.shijicloudstack.model.dto.menu.MenuPageDto;
import com.lzh.shijicloudstack.model.entity.IngredientCategory;
import com.lzh.shijicloudstack.model.entity.Menu;
import com.lzh.shijicloudstack.template.PageAbstractTemplate;
import org.springframework.stereotype.Service;
import com.lzh.shijicloudstack.service.MenuService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 菜单权限表(SysMenu)表服务实现类
 *
 * @author luzhiheng
 * @since 2023-12-28 16:14:28
 */
@Service("menuService")
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Override
    public Boolean delete(Long id) {
        baseMapper.deleteById(id);
        return true;
    }

    @Override
    public Boolean updateMenu(Menu menu) {
        Long id = menu.getId();
        if (id == null) {
            baseMapper.insert(menu);
        } else {
            Menu menu1 = baseMapper.selectById(id);
            if (menu1 == null) {
                return false;
            }
            menu1.setMenuName(menu.getMenuName());
            menu1.setPath(menu.getPath());
            menu1.setOrderNum(menu.getOrderNum());
            menu1.setMenuType(menu.getMenuType());
            menu1.setStatus(menu.getStatus());
            menu1.setParentId(menu.getParentId());
            menu1.setRemark(menu.getRemark());
        }
        return null;
    }

    @Override
    public Map<String, Object> pageList(MenuPageDto dto) {
        Map<String, Object> otherQueryMap = new HashMap<>();
        PageAbstractTemplate template = new PageAbstractTemplate() {
            @Override
            public Integer countTotal() {
                return baseMapper.countList(this.getQueryMap(dto.getPageModel(), otherQueryMap));
            }

            @Override
            public List<Map<String, Object>> queryPageList() {
                List<Map<String, Object>> pageList = baseMapper.pageList(this.queryMap);
                return pageList;
            }
        };
        return template.queryPage();
    }
}

